package com.borened.common.core.mybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.enums.SqlLike;

import java.util.Arrays;
import java.util.List;

import static com.baomidou.mybatisplus.core.enums.SqlKeyword.LIKE;

/**
 * 简单的字段运算查询条件包装类。继承QueryWrapper 以实现内部方法。
 * <pre>
 *     搭配MpWrapper注解实现快速查询.
 *     {@linkplain BaseController#buildWrapper(com.borened.common.core.base.BaseQuery)}
 * </pre>
 *
 * @author: cch
 * @since: 2022/3/3
 */
public class MpQueryWrapper<T> extends QueryWrapper<T> {
    /**
     * 支持的查询条件 运算符类型
     */
    public static final List<SqlKeyword> SUPPORTED_KEYWORDS = Arrays.asList(
            SqlKeyword.EQ, SqlKeyword.NE, SqlKeyword.GT, SqlKeyword.LT,
            SqlKeyword.GE, SqlKeyword.LE, SqlKeyword.IN, SqlKeyword.LIKE
    );
    private static final long serialVersionUID = 6885314288617831224L;

    public MpQueryWrapper() {
        super(null);
    }

    public void addCondition(String column, SqlKeyword sqlKeyword, Object val) {
        if (sqlKeyword == LIKE) {
            super.likeValue(true, sqlKeyword, column, val, SqlLike.DEFAULT);
            return;
        }
        super.addCondition(true, column, sqlKeyword, val);
    }

    @Override
    public QueryWrapper<T> addCondition(boolean condition, String column, SqlKeyword sqlKeyword, Object val) {
        return super.addCondition(condition, column, sqlKeyword, val);
    }
}